package find;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

import utils.BracketUtils;
public class Ex448 {
    class Solution1 {
        //二分查找，查找中点
        public List<Integer> findDisappearedNumbers(int[] nums) {
            int len = nums.length, max = len;
            // for (int i = 0; i < len; i++) max = Math.max(max, nums[i]);
            int mid = max / 2;
            return null;
        }
    }

    class Solution2 {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            List<Integer> res = new ArrayList<>();
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != (i + 1) && nums[nums[i] - 1] != nums[i]) {
                    int t = nums[nums[i] - 1];
                    nums[nums[i] - 1] = nums[i];
                    nums[i] = t;
                    i--; //交换之后，需要重新考察新交换来的树
                }
            }
           
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != i + 1) res.add(i + 1);
            }
            return res;
        }
    }

    class Solution {
        public List<Integer> findDisappearedNumbers(int[] nums) {
            //访问到一个元素，将其本身属于的位置乘以-1，表示已经存在，第二次遍历找到正数，返回本来的元素
            int idx;
            for (int i = 0; i < nums.length; i++) {
                if (nums[(idx = Math.abs(nums[i]) - 1)] > 0) nums[idx] *= -1;
            }

            List<Integer> res = new ArrayList<>();
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] > 0) res.add(i + 1);
            }
            return res;
        }
    }

    @Test
    public void test() {
        int[] nums = new int[]{4,3,2,7,8,2,3,1};
        nums = Stream.iterate(1, x -> x + 1).limit(100).mapToInt(Integer::valueOf).toArray();
        List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
        Collections.shuffle(list);
        nums = list.stream().mapToInt(Integer::valueOf).toArray();
        Solution s = new Solution();
        System.out.println(s.findDisappearedNumbers(nums));
    }
}

